﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Core.Interfaces;
using System.Net.Mail;
using Core.Common;
using System.Web;
using System.Security.Cryptography;

namespace Core.Components
{
    [Export(typeof(IEmail))]
    public class Email : IEmail
    {
        const string TO_EMAIL_ADDRESS = @"tamlm@phamgiatech.vn";
        const string FROM_EMAIL_ADDRESS = @"webmaster@phamgiatech.vn";
        [Import]
        private IConfiguration _configuration;

        public void SendEmail(string From, string Subject, string Message)
        {
            MailMessage mm = new MailMessage(From, TO_EMAIL_ADDRESS);
            mm.Subject = Subject;
            mm.Body = Message;

            Send(mm);
        }

        public void SendEmail(string To, string CC, string BCC, string Subject, string Message)
        {
            MailMessage mm = new MailMessage(FROM_EMAIL_ADDRESS, To);
            if (!string.IsNullOrEmpty(CC))
                mm.CC.Add(CC);

            if (!string.IsNullOrEmpty(BCC))
                mm.Bcc.Add(BCC);

            mm.Subject = Subject;
            mm.Body = Message;
            mm.IsBodyHtml = true;
            
            Send(mm);
        }

        public void SendEmail(string[] To, string[] CC, string[] BCC, string Subject, string Message)
        {
            MailMessage mm = new MailMessage();
            foreach (string to in To)
            {
                mm.To.Add(to);
            }
            foreach (string cc in CC)
            {
                mm.CC.Add(cc);
            }
            foreach (string bcc in BCC)
            {
                mm.Bcc.Add(bcc);
            }
            mm.From = new MailAddress(FROM_EMAIL_ADDRESS);
            mm.Subject = Subject;
            mm.Body = Message;
            mm.IsBodyHtml = true;

            Send(mm);
        }

        public void SendIndividualEmailsPerRecipient(string[] To, string Subject, string Message)
        {
            foreach (string to in To)
            {
                MailMessage mm = new MailMessage(FROM_EMAIL_ADDRESS, to);
                mm.Subject = Subject;
                mm.Body = Message;
                mm.IsBodyHtml = true;

                Send(mm);
            }
        }

        private void Send(MailMessage Message)
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Send(Message);
            //During developement we will not be sending mails
        //#if !DEBUG
            
        //#endif
        }
        public void SendPasswordReminderEmail(string To, string EncryptedPassword, string Username)
        {
            string Message = "Here is the password you requested: " + EncryptedPassword.Decrypt(Username);

            SendEmail(To, "", "", "Password Reminder", Message);
        }

        public void SendEmailAddressVerificationEmail(string Username, string To)
        {
            MEFManager.Compose(this);
            string rootURL = _configuration.GetConfigurationSetting(typeof(string), "RootURL").ToString();
            string encryptedName = HttpUtility.UrlEncode(Username.Encrypt("verify"));

            string msg = "Xin vui lòng click vào đường link bên dưới để kích hoạt tài khoản MXH Sơn Ca.<BR><BR>" +
                            "<a href=\"" + rootURL + "Accounts/VerifyEmail.aspx?a=" +
                            encryptedName + "\">" +
                            rootURL + "Accounts/VerifyEmail.aspx?a=" +
                            encryptedName + "</a>";

            SendEmail(To, "", "", "MXH Sơn Ca - Yêu cầu kích hoạt tài khoản", msg);
        }                
        
        public string RandomPassword()
        {
            string randomString = "";
            byte[] randomArray = new byte[6];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(randomArray);
            randomString = Convert.ToBase64String(randomArray);
            return randomString;
        }
    }
}
